var flag = 0;
/**
 * 校验表单
 */
function checkForm(){
	var idcard =document.getElementById("servertypename").value;
	var fileName = $('#uploadPath').val();
	var check_flag = false;
	if(idcard==""){
		alert("类型名称不能为空！");
	}else if(fileName==""){
		alert("请上传图标！");
	}else {
		if(flag==1)
			alert("您只能输入GIF,BMP,PNG,JPG,JPEG格式的文件");
		else if(flag==2)
			alert("请上传尺寸为120*120的图片！");
		else
			check_flag = true;
	}
	return check_flag;
};
/**
 * 预览图片
 */
function previewImage(file,id) {
	var MAXWIDTH = $(file).attr("imageWidth");
    var MAXHEIGHT = $(file).attr("imageHeight");
    getImageSize(document.getElementById(id),MAXWIDTH,MAXHEIGHT);
};
/**
 * 压缩图片
 */
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
    var param = {
        width: width,
        height: height
    };
    if (width > maxWidth || height > maxHeight) {
        rateWidth = width / maxWidth;
        rateHeight = height / maxHeight;
        if (rateWidth > rateHeight) {
            param.width = maxWidth;
            param.height = Math.round(height / rateWidth);
        } else {
            param.width = Math.round(width / rateHeight);
            param.height = maxHeight;
        }
    }
    return param;
};
/**
 * 校验图片尺寸
 */
function getImageSize(obj,imgWidth,imgHeight) {
    var _file = obj;
    var i = _file.value.lastIndexOf('.');
    var len = _file.value.length;
    var extEndName = _file.value.substring(i + 1, len);
    var extName = "GIF,BMP,PNG,JPG,JPEG";
    if (extName.indexOf(extEndName.toUpperCase()) == -1) {
        layer.msg("您只能输入" + extName + "格式的文件");
        flag = 1;
    } else {
        var url, image;
        if (obj.files) {
            if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
                obj.select();
                url = document.selection.createRange().text;
            }
            url = window.URL.createObjectURL(obj.files[0]);
        } else {
            url = obj.value;
            url = "file:///" + url;
        }
        image = new Image();
        image.src = url;
        image.onload = function() {
            if (image.width != imgWidth || image.height != imgHeight) {
            	layer.msg("请上传尺寸为"+imgWidth+"px*"+imgHeight+"px的图片！");
                flag = 2;
                _file.value="";
            }else{
            	flag = 0;
            }
        };
    }
};
